home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 4 code / C++ Driver / iacDriver / installDriver.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-23  |  3.3 KB  |  103 lines  |  [TEXT/MPS ]

  1. #include    "installDriver.h"
  2. /**********************************Comment*****************************************
  3.  * Main looks for a "slot" in the UnitTable into which the driver can be placed.  If
  4.  * it finds a "slot" it calls the procedure changeDRVRSlot to install the driver 
  5.  * into that slot. Note that this code assumes that the resource file is open (the 
  6.  * changeDRVESlot routine needs to get at the DRVR): since the init and the DRVR 
  7.  * are in the same file this is safe.
  8.  **********************************End Comment************************************/
  9. pascal main()
  10. {
  11. short        tsSlot;
  12.  
  13. if((tsSlot = lookForSlotInUnitTable()) != 0)
  14.     changeDRVRSlot(tsSlot);
  15. }
  16.  
  17. /**********************************Comment*****************************************
  18.  * lookForSlotInUnitTable returns a short corresponding to a valid "slot" number.  It'll
  19.  range from 48 to UnitNTryCnt.  If there are no slots available it returns 0.  Essentially what
  20.  we're doing here is starting at the END of the UnitTable, testing the value at each
  21.  long-word location to see if it's nil.  If it's nil, that means we can place our driver
  22.  at that location, so we'll return the value of the "slot".
  23.  **********************************End Comment************************************/
  24. short 
  25. lookForSlotInUnitTable()
  26. {
  27. short        slot;
  28. Ptr        theBass;
  29. long        *theVoidPtr;
  30. Boolean     foundSlot;
  31.  
  32. slot = *((short *)(UnitNtryCnt)) - 1;
  33. theBass = (Ptr) (*((long *) (UTableBase)));
  34.  
  35. foundSlot = false;
  36.  
  37. while(slot>48 && !foundSlot) 
  38.     {
  39.     theVoidPtr = (long *)(theBass + (4L * slot));
  40.     
  41.     if(*theVoidPtr == 0L)
  42.         foundSlot = true;
  43.         
  44.     slot -= 1;
  45.     }
  46.     
  47. slot += 1;
  48.  
  49. if(!foundSlot)
  50.     slot = 0;
  51.     
  52. return slot;
  53. }
  54.  
  55.  
  56. /**********************************Comment*****************************************
  57. * changeDRVRSlot installs the driver into the slot passed in.  Because we want to keep
  58. * our DRVR resource ID in the resource file the same as it ever was, we GetResInfo on
  59. * it, and then set it back later.  But, before we set it back, we set the ID of the 
  60. * DRVR resource equal to the slot found, and then call OpenDriver.  OpenDriver uses the
  61. * resource ID of the DRVR resource to place it in the UnitTable -- pretty easy huh?
  62. * The only other tricky thing is we have to Detach the resource by calling DetachResource
  63. * so the resource doesn't go away when the resource file gets closed (the Resource
  64. * Manager is our friend).  The resource file gets closed when the INIT stops executing.
  65.  **********************************End Comment************************************/
  66. void
  67. changeDRVRSlot(short slot)     
  68. {
  69. Handle        theDRVR;
  70. short            err, refNum;
  71. char            *name, DRVRname[256];
  72. short            DRVRid;
  73. ResType        DRVRType;
  74.  
  75. name = "\p.TimDriver";
  76.  
  77. if(slot != 0) {        
  78.     theDRVR = GetNamedResource('DRVR', name);
  79.     GetResInfo(theDRVR, &DRVRid, &DRVRType, &DRVRname);
  80.     SetResInfo(theDRVR, slot, 0L);
  81.      
  82.     err = OpenDriver(name, &refNum);
  83.     if(err == noErr)
  84.         {
  85.         /* detach the resources from the resource map */
  86.         DetachResource(theDRVR);
  87.  
  88.         }
  89.     /* Restores the previous resource attributes so they don't change
  90.      * from startup to startup.  We just want the in-memory copy to have
  91.      * a different ID number -- not our resource in the file        */
  92.     theDRVR = GetNamedResource('DRVR', name);
  93.     SetResInfo(theDRVR, DRVRid, nil);
  94.     }
  95. }
  96.  
  97.  
  98. short 
  99. rsrcID(short id, short DRVRRefNum)
  100. {
  101. return(0xC000 + (~DRVRRefNum << 5) + id);
  102. }
  103.